﻿<?php
/*
*火车采集器外部编程接口处理标签内容示范文件																											
*该文件内自动系统的三个参数$LabelArray $LabelCookie,$LabelUrl
*对任意采集的标签都适用请对标签内容处理后直接将该数组serialize($LabelArray)输出，
*采集器内部即可接收到该标签的内容，对比以前的接口规则，新规则可以实现标签之间的数据调用和处理														
*参数说明：																																			
  *$LabelArray    -  标签名及标签内容集合 结构如：Array('栏目id' => 2,'出处'=>  'www.locoy.com','作者'=>'火车采集器','内容'=>'<center><b>暴笑短信')  ##
  *$LabelCookie   -  对应采集中用到的Cookie值							
  *$LabelUrl      -  当前采集的页面的Url地址 
  * 特别注意:如果是处理列表页,默认页,多页时会有以下两个标签
    $LabelArray['Html']       网页的源代码,没有经过采集器处理的,直接下载后的数据.修改这里的数据,请将新值赋予$LabelArray['Html']
    $LabelArray['PageType']   值可能为 List, Pages, Content 分别代表处理列表页,多页,默认页
* @Copyright  Copyright (c) 2005-2010 http://www.locoy.com		
* @Version    LocoySpider 2010 or later								
* @Licence    Support On LocoySpider Standard and Enterprise Edition																				
*以上语句建议不更改,以下为用户操作区域  该区域只限对数组值进行操作，不得有打印输出产生，不得直接增加或删除相应标签名
*/

define("LOCOY_PLUGINS_ROOT", "D:/downloads/LocoySpider_MYSQL/Plugins/");

if($LabelArray['Html']) {
  $LabelArray['Html']='当前页面的网址为:'.$LabelUrl."\r\n页面类型为:".$LabelArray['PageType']."\r\nCookies数据为:$LabelCookie\r\n接收到的数据是:".$LabelArray['Html'];

} else {
  // 火车处理之后的数据
  //$LabelArray['内容'] = str_replace('<Ьr />', '<br />', $LabelArray['内容']);
  //$LabelArray['内容'] = preg_replace('/(\<\/?strong\>)|(<\/?em\>)/i', '', $LabelArray['内容']);
  $LabelArray['内容'] = preg_replace('/[\f\n\r\t\v]+/', ' ', $LabelArray['内容']);
  
  $match_count = preg_match_all('/(<img [^>]+\/?>)|(<br *\/?>)|(<object[^>]*>[\s\S]+?<\/object>)/i', $LabelArray['内容'], $matches, PREG_PATTERN_ORDER);
  
  $LabelArray['内容'] = preg_replace('/(<img [^>]+\/?>)|(<br *\/?>)|(<object[^>]*>[\s\S]+?<\/object>)/i', '{$!!!@@@###;}', $LabelArray['内容']);
  
  $LabelArray['内容'] = _replace_words($LabelArray['内容']);
  //$LabelArray['内容'] = _convert_str($LabelArray['内容']);
  //$LabelArray['内容'] = _do_translate($LabelArray['内容']);
  
  $LabelArray['标题'] = trim($LabelArray['标题']);
  $LabelArray['tags'] = _get_random_tags($LabelArray['标题']);
  $LabelArray['标题'] = _rebuild_title( _replace_words($LabelArray['标题']) );
  
  // 还原一些不需要替换的内容
  if ($match_count != false && $match_count != 0) {
    foreach ($matches[0] as $val) {
      $LabelArray['内容'] = preg_replace('/\{\$!!!@@@###;\}/', $val, $LabelArray['内容'], 1);
    }
  }
  
  $LabelArray['评论作者'] = file_get_contents(LOCOY_PLUGINS_ROOT . '_includes/comment_authors.txt');
  
}
//#############以上为用户操作区域#############################################################################################################################
//#############以下语句必须保留，建议不更改###################################################################################################################
//ob_clean();
echo serialize($LabelArray);

function _convert_str($str) {
  $arr_src = array('/a/u', '/c/u', '/e/u');
  $arr_target = array('а', 'с', 'е'); //俄文字符
  $arr_temp = array('@@@@', '((((', '####');
  $arr_temp_pattern = array('/@@@@/', '/\(\(\(\(/', '/####/');
  $str = strrev($str);
  $str = preg_replace($arr_src, $arr_temp, $str, 10);
  $str = strrev($str);
  $str = preg_replace($arr_temp_pattern, $arr_target, $str, 10);
  return $str;
}

function _get_random_tags($str) {
  if ($str == '') return '';
  $str = preg_replace('/&#?[a-z0-9]+;/i', '', $str); //strip html entities
  $words_arr = _get_filtered_words($str);
  foreach ($words_arr as $key => $val) {
    if ($val == "'") unset($words_arr[$key]);
    else $words_arr[$key] = str_replace("'", "", $val);
  }  
  $rand_key = array_rand($words_arr);
  return $words_arr[$rand_key];
}

function _get_filtered_words($str) {
  $filters = array();
  $result_words = array();
  $handle = @fopen(LOCOY_PLUGINS_ROOT . '_includes/en_filters_dict.txt', 'r');
  if ($handle) {
    while(!feof($handle)) {
      $filters[] = preg_replace('/\s/', '', fgets($handle));
    }
    fclose($handle);
  }
  $result_words = str_word_count($str, 1);
  foreach ($result_words as $key => $val) {
    if (in_array(strtolower($val), $filters)) unset($result_words[$key]);
  }
  return $result_words;
}

function _rebuild_title($title) {
  $word_before = _get_rand_value(LOCOY_PLUGINS_ROOT . '_includes/en_words_before.txt', 'r');
  $word_after = _get_rand_value(LOCOY_PLUGINS_ROOT . '_includes/en_words_after.txt', 'r');
  $rand_name = _get_rand_value(LOCOY_PLUGINS_ROOT. '_includes/en_names.txt', 'r');
  $phrase_before = _get_rand_value(LOCOY_PLUGINS_ROOT . '_includes/en_phrases_before.txt', 'r');
  //$phrase_after = _get_rand_value(LOCOY_PLUGINS_ROOT . '_includes/en_phrases_after.txt', 'r');
  $title_suffix = '';
  if (isset($GLOBALS['LabelArray']['title_suffix']))
    $title_suffix = $GLOBALS['LabelArray']['title_suffix'];
  $rand = rand(0, 2);
  if ($rand == 0) {
    $title = "$rand_name $word_before: $title";
  } elseif ($rand == 1) {
    $title = _get_random_tags($GLOBALS['LabelArray']['内容']) . ': ' . $title;
    if ($title_suffix != '')  $title .= ' - ' . $title_suffix;
  } else {
    $title = "$phrase_before: $title";
  }
  return $title;
}

function _get_rand_value($file_path, $file_mode) {
  $rand_value = '';
  $file_results = array();
  $file_line = '';
  $handle = @fopen($file_path, $file_mode);
  if ($handle) {
    while(!feof($handle)) {
      $file_line = preg_replace('/[\f\n\r\t\v]/', '', fgets($handle));
      if ($file_line != '')
        $file_results[] = $file_line;
    }
    fclose($handle);
  }
  $rand_key = array_rand($file_results);
  $rand_value = $file_results[$rand_key];
  return $rand_value;
}

// 近义词替换
function _replace_words($instr) {
  $file_line = '';
  $file_results = array();
  $handle = @fopen(LOCOY_PLUGINS_ROOT . '_includes/en_replace_words.txt', 'r');
  if ($handle) {
    while(!feof($handle)) {
      $file_line = preg_replace('/[ \f\n\r\t\v]/', '', fgets($handle));
      if ($file_line != '')
        $file_results[] = $file_line;
    }
    fclose($handle);
  }
  
  foreach ($file_results as $result) {
    $result_arr = explode('|', trim($result));
    //return count($result_arr);
    $word_0 = $result_arr[0];
    $word_1 = $result_arr[1];
    if (strpos($instr, $word_0)) {
      //$instr = str_replace($word_0, $word_1, $instr);
      $instr = preg_replace("/([^a-z\'\-])$word_0([^a-z\-\'])/i", "$1{$word_1}$2", $instr);
    } elseif (strpos($instr, $word_1)) {
      //$instr = str_replace($reslut[1], $result[0], $instr);    
    }
  }
  
  return $instr;
  
}

function _do_translate($instr) {
  //require(LOCOY_PLUGINS_ROOT . "_gtranslate/GTranslate.php");
  global $LabelArray;
  $translate_string = $instr;  
  $gt = new Gtranslate;
  //$LabelArray['test'] = $gt->available_languages;
	$translate_string = $gt->french_to_english($translate_string);
	//$translate_string = $gt->french_to_german($translate_string);
	//$translate_string = $gt->german_to_english($translate_string);

  return $translate_string;
}

class GTranslateException extends Exception
{
	public function __construct($string) {
		parent::__construct($string, 0);
	}

	public function __toString() {
		return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
	}
}

class GTranslate {
	/**
	* Google Translate(TM) Api endpoint
	* @access private
	* @var String 
	*/
  
	private $url = "http://ajax.googleapis.com/ajax/services/language/translate";
	
        /**
        * Google Translate (TM) Api Version
        * @access private
        * @var String 
        */	
	private $api_version = "1.0";

        /**
        * Comunication Transport Method
 	* Available: http / curl
        * @access private
        * @var String 
        */
	private $request_type = "http";

        /**
        * Path to available languages file
        * @access private
        * @var String 
        */
	private $available_languages_file 	= "D:/downloads/LocoySpiderV2010_Standard/Plugins/_gtranslate_languages.ini";
	
        /**
        * Holder to the parse of the ini file
        * @access private
        * @var Array
        */
	private $available_languages = array();

	/**
	* Google Translate api key
 	* @access private 
	* @var string
	*/
	private $api_key = null;

        /**
        * Constructor sets up {@link $available_languages}
        */
	public function __construct()
	{
		$this->available_languages = parse_ini_file('D:/downloads/LocoySpiderV2010_Standard/Plugins/_gtranslate_languages.ini');
    //$this->available_languages = array('ENGLISH' => 'en', 'FRENCH' => 'fr', 'GERMAN' => 'de');

	}

        /**
        * URL Formater to use on request
        * @access private
        * @param array $lang_pair
	* @param array $string
	* "returns String $url
        */

	private function urlFormat($lang_pair,$string)
	{
		$parameters = array(
			"v" => $this->api_version,
			"q" => $string,
			"langpair"=> implode("|",$lang_pair)
		);

		if(!empty($this->api_key))
		{
			$parameters["key"] = $this->api_key;
		}

		$url  = "";

		foreach($parameters as $k=>$p)
		{
			$url 	.=	$k."=".urlencode($p)."&";
		}
		return $url;
	}

	/**
	* Define the request type
	* @access public
	* @param string $request_type
	* return boolean
	*/
	public function setRequestType($request_type = 'http') {
  		if (!empty($request_type)) {
	    		$this->request_type = $request_type;
			return true;
  		}
		return false;
	}

	/**
	* Define the Google Translate Api Key
 	* @access public
	* @param string $api_key
	* return boolean
	*/
	public function setApiKey($api_key) {
  		if (!empty($api_key)) {
	    		$this->api_key = $api_key;
			return true;
  		}
		return false;
	}
	
        /**
        * Query the Google(TM) endpoint 
        * @access private
        * @param array $lang_pair
        * @param array $string
        * returns String $response
        */

	public function query($lang_pair,$string)
	{
  
		$query_url = $this->urlFormat($lang_pair,$string);
    
		$response = $this->{"request".ucwords($this->request_type)}($query_url);
    
		return $response;
	}

        /**
        * Query Wrapper for Http Transport 
        * @access private
        * @param String $url
        * returns String $response
        */

	private function requestHttp($url)
	{
		return GTranslate::evalResponse(json_decode(file_get_contents($this->url."?".$url)));
	}

        /**     
        * Query Wrapper for Curl Transport 
        * @access private
        * @param String $url
        * returns String $response
        */

	private function requestCurl($url)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $this->url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_REFERER, !empty($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : "");
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $url);
		$body = curl_exec($ch);
		curl_close($ch);
		return GTranslate::evalResponse(json_decode($body));
	}

        /**     
        * Response Evaluator, validates the response
	* Throws an exception on error 
        * @access private
        * @param String $json_response
        * returns String $response
        */

	private function evalResponse($json_response)
	{
		switch($json_response->responseStatus)
		{
			case 200:
				return $json_response->responseData->translatedText;
				break;
			default:
				throw new GTranslateException("Unable to perform Translation:".$json_response->responseDetails);
			break;
		}
	}


        /**     
        * Validates if the language pair is valid
        * Throws an exception on error 
        * @access private
        * @param Array $languages
        * returns Array $response Array with formated languages pair
        */

	private function isValidLanguage($languages)
	{
		$language_list 	= $this->available_languages;

		$languages 		= 	array_map( "strtolower", $languages );
		$language_list_v  	= 	array_map( "strtolower", array_values($language_list) );
		$language_list_k 	= 	array_map( "strtolower", array_keys($language_list) );
		$valid_languages 	= 	false;
		if( TRUE == in_array($languages[0],$language_list_v) AND TRUE == in_array($languages[1],$language_list_v) )
		{
			$valid_languages 	= 	true;	
		}

		if( FALSE === $valid_languages AND TRUE == in_array($languages[0],$language_list_k) AND TRUE == in_array($languages[1],$language_list_k) )
		{
			$languages 	= 	array($language_list[strtoupper($languages[0])],$language_list[strtoupper($languages[1])]);
			$valid_languages        =       true;
		}

		if( FALSE === $valid_languages )
		{
			throw new GTranslateException("Unsupported languages (".$languages[0].",".$languages[1].")");
		}

		return $languages;
	}

        /**     
        * Magic method to understande translation comman
	* Evaluates methods like language_to_language
        * @access public
	* @param String $name
        * @param Array $args
        * returns String $response Translated Text
        */


	public function __call($name,$args)
	{
		$languages_list 	= 	explode("_to_",strtolower($name));
		$languages = $this->isValidLanguage($languages_list);

		$string 	= 	$args[0];
		return $this->query($languages,$string);
	}
}

?>
